<template>
  <view>
    <button @tap="upload">上传</button>
    <image-cropper
      :src="tempFilePath"
      @confirm="confirm"
      @cancel="cancel"

      :show-reset-btn="false"
    ></image-cropper>
    <image
      :src="cropFilePath"
      mode="aspectFit"
      style="width: 100%;"
    ></image>
  </view>
</template>

<script>

import ImageCropper from "@/components/invinbg-image-cropper/invinbg-image-cropper.vue";
export default {
  onLoad(opt) {
    console.log(opt)
    setTimeout(() => {
      this.tempFilePath = opt.logoImg
    }, 1);

  },
  data() {
    return {
      tempFilePath: '',
      cropFilePath: '',
    }
  },
  components: { ImageCropper },
  methods: {
    upload() {
      uni.chooseImage({
        count: 1, //默认9
        sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album'], //从相册选择
        success: (res) => {
          this.tempFilePath = res.tempFilePaths.shift()
          console.log(res)

        }
      });
    },
    confirm(e) {
      console.log(e)
      this.tempFilePath = ''
      this.cropFilePath = e.detail.tempFilePath
      console.log(this.cropFilePath)
      uni.showLoading({
        title: '加载中'
      });
      uni.uploadFile({
        url: `${this.$base}/seller/file/uploadLogoAva`, //仅为示例，非真实的接口地址
        filePath: this.cropFilePath,
        name: 'file',
        fileType: 'image',
        formData: {
          ruleGroupId: '3462e1f4cf1s0408f3f352f3fdb64f20',
          busType: 1201
        },
        header: {
          Authorization: uni.getStorageSync('sessionId')
        },
        success: (uploadFileRes) => {
          let res = JSON.parse(uploadFileRes.data)
          console.log(res)
          if (res.code == 200) {

          } else {
            return uni.showToast({ title: res.msg, icon: 'none' })
          }
          uni.hideLoading()
        }
      })
    },
    cancel() {
      console.log('canceled')
      uni.redirectTo({
        url: '/pages/StoreManage/index'
      });
    }
  },
}

</script>